//
//  WallpaperView.m
//  Pictorial
//
//  Created by SKY on 15/12/29.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "WallpaperView.h"
#import "WallpaperDownloadView.h"

#import "WallpaperModel.h"
#import "Net.h"
#import "ModelTool.h"

@interface WallpaperView ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
{
    int _page;
    int _pageCount;
}
@property (nonatomic,strong) NSMutableArray *allData;
@property (nonatomic,strong) NSMutableArray *pageArray;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UICollectionView *collectionView;

@end

static NSString *cellID = @"cell";

@implementation WallpaperView

-(void)willMoveToSuperview:(UIView *)newSuperview{
    self.frame = newSuperview.bounds;
    
    _page = 0;
    [self.pageArray addObject:[NSString stringWithFormat:@"%d",_page]];
    
    [self createUI];
    [self downloadData];
}
-(void)downloadData{
    
    [Net getRequestHttpURL:WALLPAPER_URL view:self completation:^(id object) {
        NSArray *array = object;
        for (NSDictionary *dict in array) {
//            [ModelTool createModelWithDictionary:dict];
            WallpaperModel *model = [WallpaperModel objectWithDict:dict];
            [self.allData addObject:model];
        }
        [self reloadData:_page];
    }];
}

- (void)reloadData:(int)page {
    
    if (page * _pageCount > self.allData.count) {
        return;
    }
    
    int count = (page + 1) * _pageCount > self.allData.count ? (self.allData.count % _pageCount):(_pageCount);
    for (int index = page * _pageCount; index < count + page * _pageCount; index ++) {
        [self.dataArray addObject:self.allData[index]];
    }
    
    [self.collectionView reloadData];

}

-(void)createUI{
    
    CGFloat interval = 5;
    CGFloat w = (WIDTH - 3 * interval) / 2;
    CGFloat h = w * 1.5;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(w, h);
    layout.minimumInteritemSpacing = interval;
    layout.minimumLineSpacing = interval;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(interval, 80, WIDTH - interval * 2, HEIGHT - 80) collectionViewLayout:layout];
    [self addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    
    _pageCount = (self.collectionView.frame.size.height / (h + interval) + 1) * 2;
    
}
-(NSMutableArray *)allData{
    if (_allData == nil) {
        _allData = [NSMutableArray array];
    }
    return _allData;
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)pageArray{
    if (_pageArray == nil) {
        _pageArray = [NSMutableArray array];
    }
    return _pageArray;
}
- (IBAction)back:(id)sender {
    [_delegate back];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
        _page ++;
        for (NSString *index in self.pageArray) {
            if ([index isEqualToString:([NSString stringWithFormat:@"%d",_page])]) {
                return;
            }
        }
        [self.pageArray addObject:[NSString stringWithFormat:@"%d",_page]];
        [self reloadData:_page];
    }
}

#pragma mark - UICollectionView
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WallpaperModel *model = self.dataArray[indexPath.item];
    NSString *pictureString = model.ios_wallpaper_url;
    
    WallpaperDownloadView *downloadView = [WallpaperDownloadView nibInitializtion];
    [self addSubview:downloadView];
    
    downloadView.pictureString = pictureString;
    downloadView.title = model.title;
    downloadView.desc = model.destination;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WallpaperModel *model = self.dataArray[indexPath.item];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    for(UIView *subView in cell.contentView.subviews){
        [subView removeFromSuperview];
    }
    
    UIImageView *imageView = [UIImageView imageViewWithNetImage:model.ios_wallpaper_url frame:cell.bounds];
    [cell.contentView addSubview:imageView];
    
    NSString *day = [model.publish_date day];
    NSString *month = [model.publish_date month];
    UILabel *dayLabel = [UILabel labelWithText:day fontSize:30 frame:CGRectMake(20, cell.frame.size.height / 2 - 30, cell.frame.size.width - 20 * 2, 30) color:[UIColor whiteColor] textAlignment:1];
    [cell.contentView addSubview:dayLabel];
    
    UILabel *monthLabel = [UILabel labelWithText:month fontSize:14 frame:CGRectMake(20, cell.frame.size.height / 2, cell.frame.size.width - 20 * 2, 30) color:[UIColor whiteColor] textAlignment:1];
    [cell.contentView addSubview:monthLabel];
    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
@end
